#pragma GCC optimize("Ofast", "unroll-loops")
#include<bits/stdc++.h>
using namespace std; using ll = long long;
#define For(i, j, k) for ( int i = (j) ; i <= (k) ; i++ )
#define Fol(i, j, k) for ( int i = (j) ; i >= (k) ; i-- )
struct barrett
{
	int p; __uint128_t b;
	inline barrett() {}
	inline barrett(int p) : p(p), b(( (__uint128_t)1 << 64 ) / p) {}
	inline friend istream &operator>>(istream &is, barrett &x) { int p; is >> p, x = barrett(p); return is; }
	template < class T > inline friend T operator%(T x, const barrett &y) { return ( x -= (T)( ( x * y.b ) >> 64 ) * y.p ) < y.p ? x : x - y.p; }
	template < class T > inline friend T &operator%=(T &x, const barrett &y) { return x = x % y; }
};
constexpr int mod = 998244353;
inline void add(int &x) { x >= mod && ( x -= mod ); }
inline void sub(int &x) { x < 0 && ( x += mod ); }
inline int qpow(int x, int y) { int z = 1; while ( y ) y & 1 && ( z = (ll)z * x % mod ), ( y >>= 1 ) && ( x = (ll)x * x % mod ); return z; }
inline int inv(int x) { return qpow(x, mod - 2); }
using ld = long double; constexpr ld eps = 1e-15l, inf = numeric_limits < ld >::max();
struct info
{
	int k, b;
	inline info() {}
	inline info(int k, int b) : k(k), b(b) {}
	inline info &operator+=(const info &o) { return add(k += o.k), add(b += o.b), *this; }
}	cur, res; int n, m, oo, aa, bb, cc, cnt[10000009]; barrett mm; ld l, r, md, nw, mn;
inline bool chk(ld x)
{
	nw = 2; For(i, 1, n) if ( ( nw = 1 - max(0.l, x + x - nw) - ( cnt[i] != 1 ? x : 0 ) ) < 0 ) return false;
	return true;
}
inline int calc(ld x)
{
	nw = 2, cur = info(0, 2), mn = inf;
	For(i, 1, n)
	{
		if ( x + x - nw > 0 ) nw += 1 - x - x, cur += info(mod - 2, 1);
		else nw = 1, cur = info(0, 1);
		if ( cnt[i] != 1 ) nw -= x, cur += info(mod - 1, 0);
		if ( fabsl(nw) < mn ) mn = fabsl(nw), res = cur;
	}
	return (ll)( mod - res.b ) * inv(res.k) % mod;
}
inline void work()
{
	cin >> n >> oo >> aa >> bb >> cc >> m, mm = barrett(m), fill(cnt + 1, cnt + m + 1, 0), l = 1.l / 3, r = 1;
	For(i, 1, n) cnt[oo = ( ( (ll)aa * oo + bb ) % mm * oo + cc ) % mm + 1]++;
	n = 0; For(i, 1, m) if ( cnt[i] ) { cnt[++n] = cnt[i]; if ( n != 1 && cnt[n] != 1 ) r = .5l; }
	while ( r - l > eps ) chk(md = ( l + r ) / 2) ? l = md : r = md;
	md = ( l + r ) / 2, cout << fixed << setprecision(9) << md << ' ' << calc(md) << '\n';
}
int main()
{
	freopen("guess.in", "r", stdin), freopen("guess.out", "w", stdout);
	cin.tie(nullptr) -> sync_with_stdio(false); 
	int t; cin >> t; while ( t-- ) work(); return 0;
}